﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIGERP_Negocio;
using SIGERP_Entidades;

namespace SIGERP_Presentacion
{
    public partial class frmPaisActualizar : Form
    {
        private ErrorProvider ep = new ErrorProvider();
        private Pais p;

        public frmPaisActualizar()
        {
            InitializeComponent();
        }

        public frmPaisActualizar(Pais pais)
        {
            p = pais;
            InitializeComponent();
            txtIdPais.Text = p.id.ToString();
            txtNomPais.Text = p.descripcion;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ep.Clear();
            
            if (txtNomPais.Text.Equals(""))
            {
                ep.SetError(txtNomPais, "Este campo es obligatorio");
                txtNomPais.Focus();
                return;
            }

            if (!p.descripcion.Equals(txtNomPais.Text) && Paises.Existe(txtNomPais.Text.ToUpper()).Count != 0)
            {
                MessageBox.Show("El pais ingresado ya existe", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            }
            else
            {
                actualizarPais();
            }         
 
        }

        private void actualizarPais()
        {
            p.id = Convert.ToInt32(txtIdPais.Text);
            p.descripcion = txtNomPais.Text.ToUpper();
            Paises.Actualizar(p);
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void toolStripAyuda_Click(object sender, EventArgs e)
        {
            Utilities.mostrarAyuda(this, (this.GetType().ToString() + ".htm").Replace("SIGERP_Presentacion.", ""));
        }
    }
}
